package com.czh.zhiwen.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.czh.zhiwen.model.dto.question.QuestionContentDTO;
import com.czh.zhiwen.model.entity.App;
import com.czh.zhiwen.model.entity.Question;
import com.czh.zhiwen.model.entity.ScoringResult;
import com.czh.zhiwen.model.entity.UserAnswer;
import com.czh.zhiwen.model.vo.QuestionVO;
import com.czh.zhiwen.service.QuestionService;
import com.czh.zhiwen.service.ScoringResultService;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@ScoringStrategyConfig(appType = 1,scoringStrategy = 0)
public class TestScoringStrategyImpl implements ScoringStrategy {

    @Resource
    private  QuestionService questionService;


    @Resource
    private ScoringResultService scoringResultService;

    /**
     * 根据用户选择的答案，对比题目的答案来进行评分
     * @param choices
     * @param app
     * @return
     * @throws Exception
     */
    @Override
    public UserAnswer doScore(List<String> choices, App app) throws Exception {
        // 1、根据id查询题目和题目的答案
        Question question = questionService.getOne(
                Wrappers.lambdaQuery(Question.class)
                        .eq(Question::getAppId, app.getId()));

        // 2、根据AppId获取用户的答案列表
        List<ScoringResult> list = scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class)
                .eq(ScoringResult::getAppId, app.getId()));

        // 3、根据用户选择的答案和题目的答案来进行评分
        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContentDTO> questionContent = questionVO.getQuestionContent();
        //统计每个选项被选择的次数
        Map<String,Integer> optionCount = new HashMap();
        //遍历题目内容
        for (QuestionContentDTO questionContentDTO : questionContent) {
            //遍历用户的答案
            for (String answer  : choices) {
                //遍历题目的选项
                for(QuestionContentDTO.Option option: questionContentDTO.getOptions()){
                    //如果用户选择的答案和题目的答案一致
                    if(option.getKey().equals(answer)){
                        String relust = option.getResult();
                        //如果选项统计中不存在该选项，则初始化
                        if(!optionCount.containsKey(relust)){
                            optionCount.put(relust,0);
                        }
                        //如果选项统计中存在该选项，则次数+1
                        optionCount.put(relust,optionCount.get(relust)+1);
                    }
                }
            }
        }
        int maxCount = 0;
        ScoringResult maxScoringResult = list.get(0);
        //遍历用户的答案列表
        for(ScoringResult result : list){
            List<String> resulitpro = JSONUtil.toList(result.getResultProp(), String.class);
            //统计每个选项被选择的次数
            int score = resulitpro.stream()
                    .mapToInt(prop -> optionCount.getOrDefault(prop, 0))
                    .sum();
            //如果当前选项被选择的次数大于最大值，则更新最大值和对应的答案
            if(score > maxCount){
                maxCount = score;
                maxScoringResult = result;
            }
        }

        //返回评分结果
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());

        return userAnswer;
    }
}
